/*
 * @Author: shijianxin
 * @Date: 2022-04-08 17:39:22
 * @Description: 方块形状类
 */
import { SquareGroup } from "./SquareGroup";
import { Point, Shape } from "./types";
import { getRandom } from "./utils";

/**
 * T字型
 */
export class TShape extends SquareGroup {
  constructor(_centerPoint: Point, _color: string) {
    super(
      [
        { x: -1, y: 0 },
        { x: 0, y: 0 },
        { x: 1, y: 0 },
        { x: 0, y: -1 },
      ],
      _centerPoint,
      _color
    );
  }
}

/**
 * L型
 */
export class LShape extends SquareGroup {
  constructor(_centerPoint: Point, _color: string) {
    super(
      [
        { x: -2, y: 0 },
        { x: -1, y: 0 },
        { x: 0, y: 0 },
        { x: 0, y: -1 },
      ],
      _centerPoint,
      _color
    );
  }
}

/**
 * 反向L型
 */
export class LMirrorShape extends SquareGroup {
  constructor(_centerPoint: Point, _color: string) {
    super(
      [
        { x: 2, y: 0 },
        { x: 1, y: 0 },
        { x: 0, y: 0 },
        { x: 0, y: -1 },
      ],
      _centerPoint,
      _color
    );
  }
}

/**
 * s型
 */
export class SShape extends SquareGroup {
  constructor(_centerPoint: Point, _color: string) {
    super(
      [
        { x: 0, y: 0 },
        { x: 1, y: 0 },
        { x: 0, y: 1 },
        { x: -1, y: 1 },
      ],
      _centerPoint,
      _color
    );
  }

  rotate(): void {
    super.rotate();
    this.isClock = !this.isClock;
  }
}

/**
 * 反向s型
 */
export class SMirrorShape extends SquareGroup {
  constructor(_centerPoint: Point, _color: string) {
    super(
      [
        { x: 0, y: 0 },
        { x: -1, y: 0 },
        { x: 0, y: 1 },
        { x: 1, y: 1 },
      ],
      _centerPoint,
      _color
    );
  }

  rotate(): void {
    super.rotate();
    this.isClock = !this.isClock;
  }
}

/**
 * 田字型
 */
export class SquareShape extends SquareGroup {
  constructor(_centerPoint: Point, _color: string) {
    super(
      [
        { x: 0, y: 0 },
        { x: 1, y: 0 },
        { x: 0, y: 1 },
        { x: 1, y: 1 },
      ],
      _centerPoint,
      _color
    );
  }

  afterRotateShape(): Shape {
    return this.shape;
  }
}

/**
 * 长条型
 */
export class LineShape extends SquareGroup {
  constructor(_centerPoint: Point, _color: string) {
    super(
      [
        { x: -1, y: 0 },
        { x: 0, y: 0 },
        { x: 1, y: 0 },
        { x: 2, y: 0 },
      ],
      _centerPoint,
      _color
    );
  }

  rotate(): void {
    super.rotate();
    this.isClock = !this.isClock;
  }
}

export const shapes = [
  TShape,
  LShape,
  LMirrorShape,
  SShape,
  SMirrorShape,
  SquareShape,
  LineShape,
];

export const colors = ["red", "green", "blue", "orange"];

/**
 * 随机生成一个方块组，颜色随机，形状随机
 * @param centerPoint
 */
export function createTeris(centerPoint: Point) {
  // 随机获取一个形状
  let index = getRandom(0, shapes.length - 1);
  const shape = shapes[index];
  // 随机获取一个颜色
  index = getRandom(0, colors.length - 1);
  const color = colors[index];
  // 返回一个方块组类
  return new shape(centerPoint, color);
}
